#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a),i##__END__=(b);i<=i##__END__;++i)
#define drep(i,a,b) for (int i=(a),i##__END__=(b);i>=i##__END__;--i)
using namespace std;
using ll = long long;

void ofile(string s){ // remember!!!
  freopen((s+".in").c_str(),"r",stdin);
  freopen((s+".out").c_str(),"w",stdout);
}

int n,N,m,_[200010],a[200010],q[200010],ty[800010],pos[800010],rt[25],res[200010],np[25];
bool tag[800010];
ll sum[800010];
char s[25][200010];

void bld(int p,int l,int r,int d){
  if (l^r){
    int mid=(l+r)>>1;
    sum[p]=1ll*(r-l+1)*(l+r)/2;
    tag[p]=0;
    if (l==1)rt[__builtin_ctz(r)]=p;
    bld(p<<1,l,mid,d-1);
    bld(p<<1|1,mid+1,r,d-1);
    ty[p]=s[d][np[d]++]^48;
    // cerr<<l<<' '<<r<<' '<<d<<' '<<ty[p]<<'\n';
  }else
    sum[p]=l,pos[l]=p;
}
void clr(int p,int l,int r){
  if (l^r){
    int mid=(l+r)>>1;
    sum[p]=1ll*(r-l+1)*(l+r)/2;
    tag[p]=0;
    clr(p<<1,l,mid);
    clr(p<<1|1,mid+1,r);
  }else
    sum[p]=l;
}
void ins(int p,int x,int y,int D){
  bool ok=1,full=1;
  int r=p;
  rep(d,1,D){
    // cerr<<p<<' '<<ty[p]<<' '<<y<<'\n';
    if (tag[p])break;
    if (ty[p>>1]==p%2){
      // cerr<<x<<' '<<p<<'\n';
      if (y>=d){
        if (full){
          tag[p^1]=1;
          sum[p^1]=0;
        }
      }else{
        ok=0;
        break;
      }
    }else if (p%2==0)
      full=0;
    // cerr<<x<<" OK\n";
    p>>=1;
  }
  p=r;
  if (!ok)sum[p]=0;
  while (p!=rt[D]){
    p>>=1;
    sum[p]=sum[p<<1]+sum[p<<1|1];
  }
}
void solve(){
  int x[4];
  rep(i,0,3)scanf("%d",&x[i]);
  rep(i,1,n)a[i]=_[i]^x[i%4];
  // rep(i,1,n)cerr<<a[i]<<' ';
  // cerr<<'\n';
  res[1]=1;
  rep(dep,1,20){
    if ((1<<dep)>N)break;
    rep(i,1,min(1<<dep,n)){
      ins(pos[i],i,a[i],dep);
      if (i>(1<<(dep-1)))res[i]=sum[rt[dep]];
    }
    clr(rt[dep],1,1<<dep);
  }
  // rep(i,1,n)cerr<<res[i]<<' ';
  // cerr<<'\n';
  ll out=0;
  rep(i,1,m)out^=1ll*i*res[q[i]];
  printf("%lld\n",out);
}

int main(){
  ofile("arena");
  scanf("%d%d",&n,&m);
  rep(i,1,n)scanf("%d",&_[i]);
  rep(i,1,m)scanf("%d",&q[i]);
  N=1;
  while (N<n)N<<=1;
  rep(i,1,20)if ((1<<i)<=N)scanf("%s",s[i]);
  bld(1,1,N,__builtin_ctz(N));
  int tt;
  scanf("%d",&tt);
  while (tt--)solve();
  return 0;
}